# -*- coding:utf8 -*-
'''
Created on 2011-5-14

@author: Vendar
'''

from PyQt4 import QtGui,QtCore
from ui import MainWindow_ui
import DocsData
import LoginDlg
import PreferencesDlg
import AboutDlg

class MainFrame(QtGui.QMainWindow):
    #account changed signal,with param: username,password
    Signal_AccountChanged = QtCore.pyqtSignal(str,str)
    
    def __init__(self,parent=None,wflags=0):
        super(QtGui.QMainWindow,self).__init__(parent)
        
        #init ui
        self.ui = MainWindow_ui.Ui_MainWindow()
        self.ui.setupUi(self)
        
        #gdata clients, and variables
        self.GDocs = DocsData.DocsData()
        self.Settings = PreferencesDlg.PreferencesDlg()
        
        #setup connections
        self.ui.contentList.itemClicked.connect(self.OnItemClicked)
        self.ui.actionExit.triggered.connect(self.ActExit)
        self.ui.actionDocument.triggered.connect(self.ActDocument)
        self.ui.actionPreferences.triggered.connect(self.ActPreferences)
        self.ui.actionUpdate.triggered.connect(self.ActUpdate)
        self.ui.actionAbout.triggered.connect(self.ActAbout)
        
        self.Settings.Signal_ReqLogin.connect(self.OnReqLogin)
        self.Signal_AccountChanged.connect(self.Settings.OnAccountChanged)
        
    def Init(self):
        return self.OnReqLogin(self)
    
    def FetchDocs(self):
        self.ui.contentList.clear()
        self.ui.content.clear()
        docs = self.GDocs.GetDocsTitles()
        for doctitle in docs:
            item = QtGui.QListWidgetItem(unicode(doctitle).decode('utf8'))
            #get system icon
            self.ui.contentList.addItem(item)
    
    @QtCore.pyqtSlot('QWidget')
    def OnReqLogin(self,whoAsked):
        dlg = LoginDlg.LoginDlg(self.Settings.GetUserName(),whoAsked)
        if dlg.exec_() == QtGui.QDialog.Accepted:
            progDlg = QtGui.QProgressDialog('Connecting','Cancel',0,0)
            progDlg.setCancelButton(None)
            progDlg.exec_()
            if self.GDocs.Login(dlg.GetUserName(), dlg.GetPassword()):
                self.Signal_AccountChanged.emit(dlg.GetUserName(),dlg.GetPassword())
                self.FetchDocs()
                progDlg.finished()
                return True
            else:
                dlg.exec_()
                progDlg.finished()
        return False
        
    @QtCore.pyqtSlot('QListWidgetItem *')
    def OnItemClicked(self,item):
        self.ui.content.setText(item.text())
        #QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(item.text()))
        
    @QtCore.pyqtSlot()
    def ActExit(self):
        QtGui.qApp.exit()
    
    @QtCore.pyqtSlot()
    def ActDocument(self):
        pass
    
    @QtCore.pyqtSlot()
    def ActPreferences(self):
        self.Settings.Show()
    
    @QtCore.pyqtSlot()
    def ActUpdate(self):
        pass
    
    @QtCore.pyqtSlot()
    def ActAbout(self):
        dlg = AboutDlg.AboutDlg(self)
        dlg.exec_()
        